home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / troubleshoot / CheckPPDSanity.py < prev    next >
Text File  |  2009-10-19  |  6KB  |  167 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Printing troubleshooter
  4.  
  5. ## Copyright (C) 2008, 2009 Red Hat, Inc.
  6. ## Copyright (C) 2008, 2009 Tim Waugh <twaugh@redhat.com>
  7.  
  8. ## This program is free software; you can redistribute it and/or modify
  9. ## it under the terms of the GNU General Public License as published by
  10. ## the Free Software Foundation; either version 2 of the License, or
  11. ## (at your option) any later version.
  12.  
  13. ## This program is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ## GNU General Public License for more details.
  17.  
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with this program; if not, write to the Free Software
  20. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. import cups
  23. import cupshelpers
  24. import installpackage
  25. import os
  26. import subprocess
  27. from timedops import TimedOperation, TimedSubprocess
  28. from base import *
  29.  
  30. class CheckPPDSanity(Question):
  31.     def __init__ (self, troubleshooter):
  32.         Question.__init__ (self, troubleshooter, "Check PPD sanity")
  33.         vbox = gtk.VBox ()
  34.         vbox.set_border_width (12)
  35.         vbox.set_spacing (12)
  36.         self.label = gtk.Label ()
  37.         self.label.set_line_wrap (True)
  38.         self.label.set_use_markup (True)
  39.         self.label.set_alignment (0, 0)
  40.         vbox.pack_start (self.label, False, False, 0)
  41.  
  42.         box = gtk.HButtonBox ()
  43.         box.set_layout (gtk.BUTTONBOX_START)
  44.         self.install_button = gtk.Button (_("Install"))
  45.         box.add (self.install_button)
  46.         # Although we want this hidden initially,
  47.         # troubleshooter.new_page will call show_all() on the widget
  48.         # we give it.  We'll need to hide this button in the display()
  49.         # callback instead.
  50.         vbox.pack_start (box, False, False, 0)
  51.  
  52.         troubleshooter.new_page (vbox, self)
  53.  
  54.     def display (self):
  55.         self.answers = {}
  56.  
  57.         answers = self.troubleshooter.answers
  58.         if not answers['cups_queue_listed']:
  59.             return False
  60.  
  61.         parent = self.troubleshooter.get_window ()
  62.         name = answers['cups_queue']
  63.         tmpf = None
  64.         try:
  65.             cups.setServer ('')
  66.             self.op = TimedOperation (cups.Connection, parent=parent)
  67.             c = self.op.run ()
  68.             self.op = TimedOperation (c.getPPD, args=(name,), parent=parent)
  69.             tmpf = self.op.run ()
  70.         except RuntimeError:
  71.             return False
  72.         except cups.IPPError:
  73.             return False
  74.  
  75.         self.install_button.hide ()
  76.         title = None
  77.         text = None
  78.         try:
  79.             ppd = cups.PPD (tmpf)
  80.             self.answers['cups_printer_ppd_valid'] = True
  81.  
  82.             def options (options_list):
  83.                 o = {}
  84.                 for option in options_list:
  85.                     o[option.keyword] = option.defchoice
  86.                 return o
  87.  
  88.             defaults = {}
  89.             for group in ppd.optionGroups:
  90.                 g = options (group.options)
  91.                 for subgroup in group.subgroups:
  92.                     g[subgroup.name] = options (subgroup.options)
  93.                 defaults[group.name] = g
  94.             self.answers['cups_printer_ppd_defaults'] = defaults
  95.         except RuntimeError:
  96.             title = _("Invalid PPD File")
  97.             self.answers['cups_printer_ppd_valid'] = False
  98.             try:
  99.                 self.op = TimedSubprocess (parent=parent,
  100.                                            args=['cupstestppd', '-rvv', tmpf],
  101.                                            stdin=file("/dev/null"),
  102.                                            stdout=subprocess.PIPE,
  103.                                            stderr=subprocess.PIPE)
  104.                 result = self.op.run ()
  105.                 self.answers['cupstestppd_output'] = result
  106.                 text = _("The PPD file for printer '%s' does not conform "
  107.                          "to the specification.  "
  108.                          "Possible reason follows:") % name
  109.                 text += '\n' + reduce (lambda x, y: x + '\n' + y, result[0])
  110.             except OSError:
  111.                 # Perhaps cupstestppd is not in the path.
  112.                 text = _("There is a problem with the PPD file for "
  113.                          "printer '%s'.") % name
  114.  
  115.         if tmpf:
  116.             os.unlink (tmpf)
  117.  
  118.         if title == None and not answers['cups_printer_remote']:
  119.             (pkgs, exes) = cupshelpers.missingPackagesAndExecutables (ppd)
  120.             self.answers['missing_pkgs_and_exes'] = (pkgs, exes)
  121.             if len (pkgs) > 0 or len (exes) > 0:
  122.                 title = _("Missing Printer Driver")
  123.                 if len (pkgs) > 0:
  124.                     try:
  125.                         self.packagekit = installpackage.PackageKit ()
  126.                     except:
  127.                         pkgs = []
  128.  
  129.                 if len (pkgs) > 0:
  130.                     self.package = pkgs[0]
  131.                     text = _("Printer '%s' requires the %s package but it "
  132.                              "is not currently installed.") % (name,
  133.                                                                self.package)
  134.                     self.install_button.show ()
  135.                 else:
  136.                     text = _("Printer '%s' requires the '%s' program but it "
  137.                              "is not currently installed.") % (name,
  138.                                                                (exes + pkgs)[0])
  139.  
  140.         if title != None:
  141.             self.label.set_markup ('<span weight="bold" size="larger">' +
  142.                                    title + '</span>\n\n' + text)
  143.  
  144.         return title != None
  145.  
  146.     def connect_signals (self, handle):
  147.         self.button_sigid = self.install_button.connect ("clicked",
  148.                                                          self.install_clicked)
  149.  
  150.     def disconnect_signals (self):
  151.         self.install_button.disconnect (self.button_sigid)
  152.  
  153.     def collect_answer (self):
  154.         return self.answers
  155.  
  156.     def cancel_operation (self):
  157.         self.op.cancel ()
  158.  
  159.     def install_clicked (self, button):
  160.         pkgs = self.answers.get('packages_installed', [])
  161.         pkgs.append (self.package)
  162.         self.answers['packages_installed'] = pkgs
  163.         try:
  164.             self.packagekit.InstallPackageName (0, 0, self.package)
  165.         except:
  166.             pass
  167.